// pages/inputedit/inputedit.js
var apiJs = require('../../utils/util.js')
var appData = getApp().globalData;
var common = require("../../utils/Conmon.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    UserName:'',
    Email:'',
    Text:'请输入昵称',
    TextReamak:'请输入6个汉字或12个英文以内的姓名',
    Bool:false,
    Type:0  //0:为修改昵称  1：为修改邮箱
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit(function () { });
    var that=this;
    var _type = options.type;
    var _title ='修改昵称';
    wx.showLoading({ title: '加载中...', mask: true});
    that.setData({
      UserName: options.nickName,
      Email: options.email,
      Type: _type
    })
    //针对邮箱
    if (_type==1){
      _title='修改邮箱';
      that.setData({
        TextReamak:'请输入邮箱地址',
        Text:'请输入邮箱'
      })
    }
    wx.setNavigationBarTitle({ title: _title });  //修改title
    wx.hideLoading();
  },

  /**
     * 提交保存
     */
  tapSave: function (event) {
    var that=this;
    var userName = that.data.UserName;
    var email = that.data.Email;
    var isPass = that.data.Bool;
    if (that.data.Type == 0) {
      if (!isPass){
        wx.showModal({ title: '警告', content: '昵称长度错误', showCancel: false })
        return;
      }
      //保存昵称
      that.SaveSubmit({ NickName: userName, UserID: wx.getStorageSync('UserInfo').UserID, CustID: appData.custID })
    }
    else
    {
      if (!isPass) {
        wx.showModal({ title: '警告', content: '邮箱格式错误', showCancel: false })
        return;
      }  
      //保存邮箱
      that.SaveSubmit({ Email: email, UserID: wx.getStorageSync('UserInfo').UserID, CustID: appData.custID })
    }
  },
  /**
   * 文本框失去焦点事件
   */
  tapBlur: function (e) {
    var that = this;
    var text = e.detail.value;
    var _bool=true;
    if (that.data.Type==0){
      var len = that.getByteLen(text);
      if (len == 0 || len > 12) {
        _bool=false;
      }
      that.setData({
        UserName: text,
        Bool: _bool
      })
    }else{
      if (!that.checkEmail(text)){
        _bool = false;
      }
      that.setData({
        Email: text,
        Bool: _bool
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
     * 提交
     * param: 参数 { "text":11, "name": 22}
     */
  SaveSubmit: function (param) {
    wx.showLoading({ title: '提交中...', mask: true });
    wx.request({
      url: appData.apiurl+"member/api/MemberCenter/Update",
      method: "POST",
      data: JSON.stringify(param),
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.code == 1200) {
          wx.hideLoading()
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          wx.redirectTo({
            url: '/pages/personalData/personalData'
          })
          console.log(res.data)
        }
      },
      fail: function (res) {
        wx.showModal({ title: '错误', content: res.data.msg, showCancel: false })
        wx.hideLoading()
        console.log(res.data)
      },
      complete: function (res) {
        console.log(res.data)
      }
    })
  },

  /**
   * 字符长度截取
   */
  getByteLen:function(val){
    var len = 0;
    for (var i = 0; i < val.length; i++) {
      var a = val.charAt(i);
      if (a.match(/[^\x00-\xff]/ig) != null) {
        len += 2;
      }
      else {
        len += 1;
      }
    }
    return len;
  },
  /***
   * 验证邮箱
   */
  checkEmail: function (email){
    var reg ='^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$';
    return apiJs.Regular(email, reg);
  }
})


